﻿using System;

namespace ProblemsSet
{
    public class Problem_85 : BaseProblem
    {
        public override object GetResult()
        {
            const int max = 2000000;
            
            var mx = (long) Math.Sqrt(4*max);
            var res = long.MaxValue;
            long rs = 0;

            for (long n = 1; n <= mx+1; n++)
            {
                for (long m = 1; m <= n; m++)
                {
                    var tmp = m*n*(n + 1)*(m + 1)/4;
                    if (Math.Abs(max - res) > Math.Abs(max - tmp))
                    {
                        res = tmp;
                        rs = n*m;
                    }
                    if (tmp >= max)
                    {
                        break;
                    }
                }
            }
            return rs;
        }

       
        public override string Problem
        {
            get
            {
                return @"By counting carefully it can be seen that a rectangular grid measuring 3 by 2 contains eighteen rectangles:


Although there exists no rectangular grid that contains exactly two million rectangles, find the area of the grid with the nearest solution.";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 2772;
            }
        }
    }
}
